package com.u.social.controller;


import com.u.social.entity.UserFollows;
import com.u.social.service.IUserFollowsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alex Wu
 * @since 2025-02-25
 */
@RestController
@RequestMapping("/user-follows")
public class UserFollowsController {

    @Autowired
    private IUserFollowsService userFollowsService;

    @PostMapping
    public ResponseEntity<Boolean> createUserFollow(@RequestBody UserFollows userFollows) {
        boolean created = userFollowsService.save(userFollows);
        return ResponseEntity.status(HttpStatus.CREATED).body(created);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserFollows> getUserFollowById(@PathVariable Long id) {
        UserFollows userFollows = userFollowsService.getById(id);
        if (userFollows == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(userFollows);
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserFollows> updateUserFollow(@PathVariable Long id, @RequestBody UserFollows userFollows) {
        boolean isUpdated = userFollowsService.updateById(userFollows);
        if (!isUpdated) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        UserFollows updatedUserFollow = userFollowsService.getById(userFollows.getId());
        return ResponseEntity.ok(updatedUserFollow);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUserFollow(@PathVariable Long id) {
        boolean deleted = userFollowsService.removeById(id);
        if (!deleted) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.noContent().build();
    }

}
